﻿using System.Collections.Generic;
using System.Data;
using MuZa.DBConnector;

namespace MuZa.MuZa
{
    internal class InfoBoard
    {
        private static List<Distance> DistanceTable = new List<Distance>();

        private static void GenerateDistanceTable()
        {
            DistanceTable.Add(new Distance("Karlshamn", "Älmhult", 77));
            DistanceTable.Add(new Distance("Karlshamn", "Copenhagen", 195));
            DistanceTable.Add(new Distance("Karlshamn", "Esbjerg", 487));
        }

        public static int GetDistance(string from, string to)
        {
            GenerateDistanceTable();
            foreach (Distance dist in DistanceTable)
            {
                if (dist.From.Equals(from) && dist.To.Equals(to))
                    return dist.KiloMeter;
            }

            return 1;
        }

        //////////////////////////////////////////////////////////////////////////////////////

        private static List<InfoProfile> InfoProfiles = new List<InfoProfile>();

        public static List<InfoProfile> getInfoProfiles()
        {
            return InfoProfiles;
        }

        public static void setInfoProfiles(List<InfoProfile> InfoProfiles)
        {
            InfoBoard.InfoProfiles = InfoProfiles;
        }

        public static void addInfoProfileToList(InfoProfile infoProf)
        {
            InfoProfiles.Add(infoProf);
        }

        public void addParticipantToList(Participant part)
        {
            var infoProf = new InfoProfile(part);
            InfoProfiles.Add(infoProf);
        }

        public static List<List<ResourceSource>> GiveInfo(Initiator init)
        {
            var partList = new List<List<ResourceSource>>();
            //        List partLists = new List();
            //        List resLists = new List();
            var temptask = new Task();

            int tasksize = init.getTasks().Count;
            int x = 0;
            while (x < tasksize) //tasksize = 1;
            {
                //            List<Participant> partList1 = new List<Participant>();
                //            List<Resource> resList1 = new List<Resource>();

                var ResPartList = new List<ResourceSource>();

                temptask = init.getTasks()[x];
                for (int i = 0; i != InfoProfiles.Count; i++)
                {
                    var tempart = new InfoProfile();
                    tempart = getInfoProfiles()[i];
                    for (int j = 0; j != tempart.getInfoParticipant().getResources().Count; j++)
                    {
                        var temres = new Resource();
                        temres = tempart.getInfoParticipant().getResources()[j];
                        if (temptask.getType() == null
                                ? temres.getType() == null
                                : temptask.getType() == temres.getType())
                        {
                            //                        resList1.Add(temres);
                            //                        partList1.Add(tempart.getInfoParticipant());

                            ResPartList.Add(
                                new ResourceSource(tempart.getInfoParticipant().getName(),
                                                   temres.getName(),
                                                   temres.getCost(),
                                                   temres.getAvailableQuantity(),
                                                   temres.getTotalDurationHours(),
                                                   tempart.getInfoParticipant().getLocation(),
                                                   tempart.getDoAb()));
                        }
                    }
                }

                partList.Add(ResPartList);
                //            partLists.Add(partList1);
                //            resLists.Add(resList1);
                x++;
            }

            // find list of suitable participants
            var temPartList = new List<List<ResourceSource>>();

            for (int i = 0; i != partList[0].Count; i++)
            {
                for (int j = 0; j != partList[1].Count; j++)
                {
                    if ((init.getTotalCost() >=
                         partList[0][i].getCost() * init.getTasks()[0].getAvailableQuantity() + partList[1][j].getCost())
                        &&
                        (init.getTotalDurationHours() >=
                         partList[0][i].getTotalDurationHours() + partList[1][j].getTotalDurationHours())
                        &&
                        (partList[0][i].getLocation() == null
                             ? partList[1][j].getLocation() == null
                             : partList[0][i].getLocation() == partList[1][j].getLocation())
                        && (init.getTasks()[0].getAvailableQuantity() <= partList[0][i].getAvailableQuantity())
                        && (init.getTasks()[1].getAvailableQuantity() <= partList[1][j].getAvailableQuantity()))
                    {
                        var temResPartList = new List<ResourceSource>();
                        temResPartList.Add(partList[0][i]);
                        temResPartList.Add(partList[1][j]);

                        temPartList.Add(temResPartList);
                    }
                }
            }

            //sorting list based on initiator's priorities

            //if fisrt priority is cost
            if (init.getPriority()[0] == "TotalCost")
            {
                //sort based on cost
                for (int i = 1; i != temPartList.Count; i++)
                {
                    int k = i;
                    for (int j = k - 1; j >= 0; j--)
                    {
                        //if cost of pair [i] is more then cost of pair [i-1]
                        if ((temPartList[k][0].getCost() * init.getTasks()[0].getAvailableQuantity() +
                             temPartList[k][1].getCost() * init.getTasks()[1].getAvailableQuantity())
                            <
                            (temPartList[j][0].getCost() * init.getTasks()[0].getAvailableQuantity() +
                             temPartList[j][1].getCost() * init.getTasks()[1].getAvailableQuantity()))
                        {
                            //replace
                            var temp = new List<ResourceSource>();
                            temp = temPartList[k];
                            temPartList[k] = temPartList[j];
                            temPartList[j] = temp;
                            k--;
                        }
                        // costs are equal
                        else
                        {
                            if ((temPartList[k][0].getCost() * init.getTasks()[0].getAvailableQuantity() +
                                 temPartList[k][1].getCost() * init.getTasks()[1].getAvailableQuantity())
                                ==
                                (temPartList[j][0].getCost() * init.getTasks()[0].getAvailableQuantity() +
                                 temPartList[j][1].getCost() * init.getTasks()[1].getAvailableQuantity()))
                            {
                                //if second priority is time
                                if (init.getPriority()[1] == "TotalDuration")
                                {
                                    //if time of pair [i] is more then time of pair [i-1]
                                    if ((temPartList[k][0].getTotalDurationHours()
                                         + temPartList[k][1].getTotalDurationHours())
                                        <
                                        (temPartList[j][0].getTotalDurationHours()
                                         + temPartList[j][1].getTotalDurationHours()))
                                    {
                                        //replace pairs
                                        var temp = new List<ResourceSource>();
                                        temp = temPartList[k];
                                        temPartList[k] = temPartList[j];
                                        temPartList[j] = temp;
                                        k--;
                                    }
                                    //if time is equal
                                    else
                                    {
                                        if ((temPartList[k][0].getTotalDurationHours() +
                                             temPartList[k][1].getTotalDurationHours())
                                            ==
                                            (temPartList[j][0].getTotalDurationHours() +
                                             temPartList[j][1].getTotalDurationHours()))
                                        {
                                            //sort by DoAb
                                            if ((temPartList[k][0].getDoA() + temPartList[j][1].getDoA())
                                                < (temPartList[j][0].getDoA() + temPartList[j][1].getDoA()))
                                            {
                                                //replace pairs
                                                var temp = new List<ResourceSource>();
                                                temp = temPartList[k];
                                                temPartList[k] = temPartList[j];
                                                temPartList[j] = temp;
                                                k--;
                                            }
                                        }
                                    }
                                }
                                //if second priority is DoAb
                                else
                                {
                                    if (init.getPriority()[1] == "DoAb")
                                    {
                                        //sort by DoAb
                                        //if DoAb of pair [i] is more then DoAb of pair [i-1]
                                        if ((temPartList[k][0].getDoA() + temPartList[j][1].getDoA())
                                            < (temPartList[j][0].getDoA() + temPartList[j][1].getDoA()))
                                        {
                                            //replace pairs
                                            var temp = new List<ResourceSource>();
                                            temp = temPartList[k];
                                            temPartList[k] = temPartList[j];
                                            temPartList[j] = temp;
                                            k--;
                                        }
                                        else
                                        {
                                            //if DoAb equal
                                            if ((temPartList[k][0].getDoA() + temPartList[j][1].getDoA())
                                                == (temPartList[j][0].getDoA() + temPartList[j][1].getDoA()))
                                            {
                                                //sort by time
                                                //id time of pair [i] is more then time of pair [i-1]
                                                if ((temPartList[k][0].getTotalDurationHours()
                                                     + temPartList[k][1].getTotalDurationHours())
                                                    <
                                                    (temPartList[j][0].getTotalDurationHours()
                                                     + temPartList[j][1].getTotalDurationHours()))
                                                {
                                                    //replace pairs
                                                    var temp = new List<ResourceSource>();
                                                    temp = temPartList[k];
                                                    temPartList[k] = temPartList[j];
                                                    temPartList[j] = temp;
                                                    k--;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else break;
                        }
                    }
                }
            }
            //if fisrt priority is time
            else
            {
                //if first prioity is time
                if (init.getPriority()[0] == "TotalDuration")
                {
                    //sort based on time
                    for (int i = 1; i != temPartList.Count; i++)
                    {
                        int k = i;
                        for (int j = k - 1; j >= 0; j--)
                        {
                            //if time of pair [i] is more then time of pair [i-1]
                            if ((temPartList[k][0].getTotalDurationHours() +
                                 temPartList[k][1].getTotalDurationHours())
                                <
                                (temPartList[j][0].getTotalDurationHours() +
                                 temPartList[j][1].getTotalDurationHours()))
                            {
                                //replace
                                var temp = new List<ResourceSource>();
                                temp = temPartList[k];
                                temPartList[k] = temPartList[j];
                                temPartList[j] = temp;
                                k--;
                            }
                            // time are equal
                            else
                            {
                                if ((temPartList[k][0].getTotalDurationHours() +
                                     temPartList[k][1].getTotalDurationHours())
                                    ==
                                    (temPartList[j][0].getTotalDurationHours() +
                                     temPartList[j][1].getTotalDurationHours()))
                                {
                                    //if second priority is cost
                                    if (init.getPriority()[1] == "TotalCost")
                                    {
                                        //if cost of pair [i] is more then cost of pair [i-1]
                                        if ((temPartList[k][0].getCost() * init.getTasks()[0].getAvailableQuantity() +
                                             temPartList[k][1].getCost() * init.getTasks()[1].getAvailableQuantity())
                                            <
                                            (temPartList[j][0].getCost() * init.getTasks()[0].getAvailableQuantity() +
                                             temPartList[j][1].getCost() * init.getTasks()[1].getAvailableQuantity()))
                                        {
                                            //replace pairs
                                            var temp = new List<ResourceSource>();
                                            temp = temPartList[k];
                                            temPartList[k] = temPartList[j];
                                            temPartList[j] = temp;
                                            k--;
                                        }
                                        //if cost is equal
                                        else
                                        {
                                            if ((temPartList[k][0].getCost() * init.getTasks()[0].getAvailableQuantity() +
                                                 temPartList[k][1].getCost() * init.getTasks()[1].getAvailableQuantity())
                                                ==
                                                (temPartList[j][0].getCost() * init.getTasks()[0].getAvailableQuantity() +
                                                 temPartList[j][1].getCost() * init.getTasks()[1].getAvailableQuantity()))
                                            {
                                                //sort by DoAb
                                                if ((temPartList[k][0].getDoA() + temPartList[j][1].getDoA())
                                                    < (temPartList[j][0].getDoA() + temPartList[j][1].getDoA()))
                                                {
                                                    //replace pairs
                                                    var temp = new List<ResourceSource>();
                                                    temp = temPartList[k];
                                                    temPartList[k] = temPartList[j];
                                                    temPartList[j] = temp;
                                                    k--;
                                                }
                                            }
                                        }
                                    }
                                    //if second priority is DoAb
                                    else
                                    {
                                        if (init.getPriority()[1] == "DoAb")
                                        {
                                            //sort by DoAb
                                            //if DoAb of pair [i] is more then DoAb of pair [i-1]
                                            if ((temPartList[k][0].getDoA() + temPartList[j][1].getDoA())
                                                < (temPartList[j][0].getDoA() + temPartList[j][1].getDoA()))
                                            {
                                                //replace pairs
                                                var temp = new List<ResourceSource>();
                                                temp = temPartList[k];
                                                temPartList[k] = temPartList[j];
                                                temPartList[j] = temp;
                                                k--;
                                            }
                                            else
                                            {
                                                //if DoAb equal
                                                if ((temPartList[k][0].getDoA() + temPartList[j][1].getDoA())
                                                    == (temPartList[j][0].getDoA() + temPartList[j][1].getDoA()))
                                                {
                                                    //sort by cost
                                                    //if cost of pair [i] is more then cost of pair [i-1]
                                                    if ((temPartList[k][0].getCost() *
                                                         init.getTasks()[0].getAvailableQuantity() +
                                                         temPartList[k][1].getCost() *
                                                         init.getTasks()[1].getAvailableQuantity())
                                                        <
                                                        (temPartList[j][0].getCost() *
                                                         init.getTasks()[0].getAvailableQuantity() +
                                                         temPartList[j][1].getCost() *
                                                         init.getTasks()[1].getAvailableQuantity()))
                                                    {
                                                        //replace pairs
                                                        var temp = new List<ResourceSource>();
                                                        temp = temPartList[k];
                                                        temPartList[k] = temPartList[j];
                                                        temPartList[j] = temp;
                                                        k--;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                else break;
                            }
                        }
                    }
                }
                //if firts priority is DoAb
                else
                {
                    //if first id DoAb
                    if (init.getPriority()[0] == "DoAb")
                    {
                        //sort based on DoAb
                        for (int i = 1; i != temPartList.Count; i++)
                        {
                            int k = i;
                            for (int j = k - 1; j >= 0; j--)
                            {
                                //if DoAb of pair [i] is more then DoAb of pair [i-1]
                                if ((temPartList[k][0].getDoA() +
                                     temPartList[k][1].getDoA())
                                    <
                                    (temPartList[j][0].getDoA() +
                                     temPartList[j][1].getDoA()))
                                {
                                    //replace
                                    var temp = new List<ResourceSource>();
                                    temp = temPartList[k];
                                    temPartList[k] = temPartList[j];
                                    temPartList[j] = temp;
                                    k--;
                                }
                                // DoAb are equal
                                else
                                {
                                    if ((temPartList[k][0].getDoA() +
                                         temPartList[k][1].getDoA())
                                        ==
                                        (temPartList[j][0].getDoA() +
                                         temPartList[j][1].getDoA()))
                                    {
                                        //if second priority is cost
                                        if (init.getPriority()[1] == "TotalCost")
                                        {
                                            //if cost of pair [i] is more then cost of pair [i-1]
                                            if ((temPartList[k][0].getCost() * init.getTasks()[0].getAvailableQuantity() +
                                                 temPartList[k][1].getCost() * init.getTasks()[1].getAvailableQuantity())
                                                <
                                                (temPartList[j][0].getCost() * init.getTasks()[0].getAvailableQuantity() +
                                                 temPartList[j][1].getCost() * init.getTasks()[1].getAvailableQuantity()))
                                            {
                                                //replace pairs
                                                var temp = new List<ResourceSource>();
                                                temp = temPartList[k];
                                                temPartList[k] = temPartList[j];
                                                temPartList[j] = temp;
                                                k--;
                                            }
                                            //if cost is equal
                                            else
                                            {
                                                if ((temPartList[k][0].getCost() *
                                                     init.getTasks()[0].getAvailableQuantity() +
                                                     temPartList[k][1].getCost() *
                                                     init.getTasks()[1].getAvailableQuantity())
                                                    ==
                                                    (temPartList[j][0].getCost() *
                                                     init.getTasks()[0].getAvailableQuantity() +
                                                     temPartList[j][1].getCost() *
                                                     init.getTasks()[1].getAvailableQuantity()))
                                                {
                                                    //sort by time
                                                    if ((temPartList[k][0].getTotalDurationHours()
                                                         + temPartList[j][1].getTotalDurationHours())
                                                        < (temPartList[j][0].getTotalDurationHours()
                                                           + temPartList[j][1].getTotalDurationHours()))
                                                    {
                                                        //replace pairs
                                                        var temp = new List<ResourceSource>();
                                                        temp = temPartList[k];
                                                        temPartList[k] = temPartList[j];
                                                        temPartList[j] = temp;
                                                        k--;
                                                    }
                                                }
                                            }
                                        }
                                        //if second priority is time
                                        else
                                        {
                                            if (init.getPriority()[1] == "TotalDuration")
                                            {
                                                //sort by time
                                                //if time of pair [i] is more then time of pair [i-1]
                                                if ((temPartList[k][0].getTotalDurationHours()
                                                     + temPartList[j][1].getTotalDurationHours())
                                                    < (temPartList[j][0].getTotalDurationHours()
                                                       + temPartList[j][1].getTotalDurationHours()))
                                                {
                                                    //replace pairs
                                                    var temp = new List<ResourceSource>();
                                                    temp = temPartList[k];
                                                    temPartList[k] = temPartList[j];
                                                    temPartList[j] = temp;
                                                    k--;
                                                }
                                                else
                                                {
                                                    //if time equal
                                                    if ((temPartList[k][0].getTotalDurationHours()
                                                         + temPartList[j][1].getTotalDurationHours())
                                                        == (temPartList[j][0].getTotalDurationHours()
                                                            + temPartList[j][1].getTotalDurationHours()))
                                                    {
                                                        //sort by cost
                                                        //if cost of pair [i] is more then cost of pair [i-1]
                                                        if ((temPartList[k][0].getCost() *
                                                             init.getTasks()[0].getAvailableQuantity() +
                                                             temPartList[k][1].getCost() *
                                                             init.getTasks()[1].getAvailableQuantity())
                                                            <
                                                            (temPartList[j][0].getCost() *
                                                             init.getTasks()[0].getAvailableQuantity() +
                                                             temPartList[j][1].getCost() *
                                                             init.getTasks()[1].getAvailableQuantity()))
                                                        {
                                                            //replace pairs
                                                            var temp = new List<ResourceSource>();
                                                            temp = temPartList[k];
                                                            temPartList[k] = temPartList[j];
                                                            temPartList[j] = temp;
                                                            k--;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    else break;
                                }
                            }
                        }
                    }
                }
            }

            return temPartList;

            //Calculate(partList);
            // Calculate The Priorities
            //        List<ResourceSource> tmpResList = new List<ResourceSource>();
            //        tmpResList = partList.get(0);
            //        for(int i = 0; i< partList.get(0).size(); i++)
            //        {
            //            //partList.get(0).get(i);
            //            for (int j = 1; j< partList.size(); j++)
            //            {
            //                //partList.get(j)
            //                for (int k = 0; k< partList.get(j).size(); k++)
            //                {
            //                    // Conditions for cheking the criteria
            //                }
            //            }
            //        }

            //        return partList;
        }

        public static DataTable GiveInfo2(DataTable dtTask, DataTable dtPriority, string initLocation, int initTotalCost,
                                          int initTotalDurationHours)
        {
            //Steps:

            ////1. Get initiator id.
            //int initId = GetData.GetInitiatorId(initName);

            ////2. Get Task list.
            //DataTable dtTask = new DataTable();
            //dtTask = GetData.GetAllTasksFromInitiator(initId);

            ////3. Get Priority list
            //DataTable dtPriority = new DataTable();
            //dtPriority = GetData.GetAllPriorityFromInitiator(initId);

            ////4. Gets lists based on the tasks from Participant resources
            //DataTable dtTask1 = new DataTable();
            //dtTask1 = GetData.GetParticipatListForTask(dtTask.Rows[0].ItemArray[3].ToString(), dtTask.Rows[0].ItemArray[1].ToString());

            //DataTable dtTask2 = new DataTable();
            //dtTask2 = GetData.GetParticipatListForTask(dtTask.Rows[1].ItemArray[3].ToString(), dtTask.Rows[0].ItemArray[1].ToString());

            //5. Calculate the pairs
            var dTable = new DataTable();
            dTable = GetData.GetResourcesForTasks(dtTask, dtPriority, initLocation, initTotalCost,
                                                  initTotalDurationHours);

            ////6. Sort the pairs
            //for (int i = 0; i < 3; i++)
            //{
            //    string sPriority = dtPriority.Rows[0].ItemArray[2].ToString();
            //    int iPriority = (int)dtPriority.Rows[0].ItemArray[1];

            //    if (sPriority == "TotalCost")
            //    {
            //    }
            //    if (sPriority == "DoAb")
            //    {
            //    }
            //    if (sPriority == "TotalDuration")
            //    {
            //    }
            //}

            return dTable;
        }

        public static void IncrementNoRbyInitiator(int p_id)
        {
            // Update in the database
            GetData.IncrementNoRbyInitiator(p_id);
        }

        public static void IncrementNoRbyParticipant(string p_name)
        {
            // Update in the database
            int p_id = GetData.GetParticipantId(p_name);
            GetData.IncrementNoRbyParticipant(p_id);
            IncrementDoAbByParticipant(p_name);
        }

        public static void IncrementNoTbyInitiator(int p_id)
        {
            //Update in the database
            GetData.IncrementNoTbyInitiator(p_id);
        }

        public static void IncrementNoTbyParticipant(string p_name)
        {
            //Update in the datebase
            int p_id = GetData.GetParticipantId(p_name);
            GetData.IncrementNoTbyParticipant(p_id);
            IncrementDoAbByParticipant(p_name);
        }

        public static void IncrementNoFTbyInitiator(int p_id)
        {
            //Update in the database
            GetData.IncrementNoFTbyInitiator(p_id);
        }

        public static void IncrementNoFTbyParticipant(string p_name)
        {
            //Update in the datebase
            int p_id = GetData.GetParticipantId(p_name);
            GetData.IncrementNoFTbyParticipant(p_id);
            IncrementDoAbByParticipant(p_name);
        }

        public static void IncrementDoAbByParticipant(string p_name)
        {
            int value = 0;
            //Update in the datebase
            int p_id = GetData.GetParticipantId(p_name);
            if (GetData.GetNoTForParticipant(p_id) != 0)
            {
                value = (GetData.GetNoFTForParticipant(p_id) / GetData.GetNoTForParticipant(p_id)) + (1 / GetData.GetNoRForParticipant(p_id));
            }
            GetData.IncrementDoAbByParticipant(p_id, value);
        }
    }

    internal class Distance
    {
        public string From { get; set; }

        public string To { get; set; }

        public int KiloMeter { get; set; }

        public int UnitCost { get; set; }

        public Distance(string from, string to, int kilometer)
        {
            this.From = from;
            this.To = to;
            this.KiloMeter = kilometer;
            //this.UnitCost = unitcost;
        }
    }
}